package com.xuecheng.manage_cms.mq;

import com.alibaba.fastjson.JSON;
import com.xuecheng.framework.domain.cms.CmsPage;
import com.xuecheng.manage_cms.config.RabbitMQConfig;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * CmsPage发送MQ消息的类
 */
@Component
public class CmsPageMQ {
    @Resource
    RabbitTemplate rabbitTemplate;
    /**
     * 向MQ发送消息
     * @param cmsPage 需要发送的CmsPage标识
     */
    public void sendPostPage(CmsPage cmsPage){
        // 创建消息对象
        Map<String, String> msg = new HashMap<>();
        msg.put("pageId",cmsPage.getPageId());
        // 转成Json串
        String jsonString = JSON.toJSONString(msg);
        rabbitTemplate.convertAndSend(RabbitMQConfig.EX_ROUTING_CMS_POSTPAGE, cmsPage.getSiteId(),jsonString);
    }
}
